package cn.bluemobi.dylan.step.ui.my.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.bluemobi.dylan.step.R;
import cn.bluemobi.dylan.step.base.BaseMvpActivity;
import cn.bluemobi.dylan.step.moder.bean.Goods;
import cn.bluemobi.dylan.step.moder.bean.MemberAccount;
import cn.bluemobi.dylan.step.moder.bean.Score;
import cn.bluemobi.dylan.step.moder.bean.User;
import cn.bluemobi.dylan.step.presenter.contract.my.ScoreContract;
import cn.bluemobi.dylan.step.presenter.my.activity.ScorePresenter;
import cn.bluemobi.dylan.step.ui.my.adapter.GoodsAdapter;
import cn.bluemobi.dylan.step.ui.my.adapter.TabScoreAdapter;
import cn.bluemobi.dylan.step.ui.my.fragment.ScoreListFragment;

/**
 * 我的积分
 */
public class ScoreActivity extends BaseMvpActivity<ScorePresenter> implements ScoreContract.View {

    public static final String tab1 = "全部";
    public static final String tab2 = "收入";
    public static final String tab3 = "支出";
    String[] tabTitle = new String[]{tab1, tab2, tab3};

    @BindView(R.id.tool_bar)
    Toolbar mToolbar;
    @BindView(R.id.toolbar_back)
    ImageView toolbar_back;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.tv_right)
    TextView tv_right;
    @BindView(R.id.collapsingToolbarLayout)
    CollapsingToolbarLayout collapsingToolbarLayout;
    @BindView(R.id.refreshLayout)
    RefreshLayout refreshLayout;

    @BindView(R.id.tv_stepcount)
    TextView tv_stepcount;
    @BindView(R.id.tab_score)
    TabLayout mTabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;

    //    private List<Goods> goodsList;
//    private GoodsAdapter adapter;  //商品
    private MemberAccount memberAccount; //积分
    private List<Fragment> fragments = new ArrayList<Fragment>();
    private TabScoreAdapter tabAdapte;
    private User user;

    @Override
    protected int getLayout() {
        return R.layout.my_score;
    }

    @Override
    protected void initView() {
        super.initView();
        initToolbar();
        initTab();
        memberAccount = (MemberAccount) getIntent().getSerializableExtra("MemberAccount");
        if (memberAccount != null) {
            tv_stepcount.setText(memberAccount.accountScore + "");
        }
        initRefreshLoadMore();
//        mPresenter.dbGetStepcountList();
//        mPresenter.httpGoods();
//        mPresenter.httpScoreList();
    }

    private void initToolbar() {
        setSupportActionBar(mToolbar);
        tv_title.setText("我的积分");
        tv_right.setVisibility(View.VISIBLE);
        tv_right.setText("积分兑换");
        tv_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(ScoreActivity.this, IntegralActivity.class);
                intent.putExtra("MemberAccount", memberAccount);
                startActivity(intent);
            }
        });
        toolbar_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void initTab() {
        ScoreListFragment fragment1 = new ScoreListFragment();
        Bundle bundle = new Bundle();
        bundle.putString("tab", tab1);
        fragment1.setArguments(bundle);

        ScoreListFragment fragment2 = new ScoreListFragment();
        Bundle bundle2 = new Bundle();
        bundle2.putString("tab", tab2);
        fragment2.setArguments(bundle2);

        ScoreListFragment fragment3 = new ScoreListFragment();
        Bundle bundle3 = new Bundle();
        bundle3.putString("tab", tab3);
        fragment3.setArguments(bundle3);

        fragments.add(fragment1);
        fragments.add(fragment2);
        fragments.add(fragment3);

        tabAdapte = new TabScoreAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(tabAdapte);
        viewPager.setOffscreenPageLimit(2);
        //TabLayout配合ViewPager有时会出现不显示Tab文字的Bug,需要按如下顺序
        mTabLayout.addTab(mTabLayout.newTab().setText(tabTitle[0]));
        mTabLayout.addTab(mTabLayout.newTab().setText(tabTitle[1]));
        mTabLayout.addTab(mTabLayout.newTab().setText(tabTitle[2]));
//        mTabLayout.addTab(mTabLayout.newTab().setText(tabTitle[2]));
        mTabLayout.setupWithViewPager(viewPager);
        mTabLayout.getTabAt(0).setText(tabTitle[0]);
        mTabLayout.getTabAt(1).setText(tabTitle[1]);
        mTabLayout.getTabAt(2).setText(tabTitle[2]);
    }


    private void initRefreshLoadMore(){
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                if(user==null){
                    mPresenter.preUser();
                }else {
                    mPresenter.getMemberAccount(user.memberId);
                }
                if(0==mTabLayout.getSelectedTabPosition()){
                    refreshScoreList(tabAdapte.tag1);
                }else if(1==mTabLayout.getSelectedTabPosition()){
                    refreshScoreList(tabAdapte.tag2);
                }else if(2==mTabLayout.getSelectedTabPosition()){
                    refreshScoreList(tabAdapte.tag3);
                }
            }
        });
        refreshLayout.setEnableLoadMore(false);
    }

    private void refreshScoreList(String tag){
        ScoreListFragment fragment = (ScoreListFragment) getSupportFragmentManager().findFragmentByTag(tag);
        if (fragment != null) {
            fragment.refreshScorList();
        }
    }

    @Override
    public void showUserInfo(User user) {
        if (user != null) {
            this.user = user;
            mPresenter.getMemberAccount(user.memberId);
        }
    }

    @Override
    public void showMemberAccount(MemberAccount memberAccount) {  //积分
        if (memberAccount != null) {
            tv_stepcount.setText(memberAccount.accountScore + "");
        }
    }

    @Override
    public void httpGoods(List<Goods> result) {

    }

    @Override
    public void httpScoreList(List<Score> result) { //积分明细

    }

    @Override
    public void httpRequestFail() {

    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }


    @Override
    public void showError(String msg) {

    }

    @Override
    public void showDialog() {
        refreshLayout.autoRefresh();
    }

    @Override
    public void closeDialog() {
        refreshLayout.finishRefresh();
    }

}
